package top.api.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import top.api.common.ResUlt;
import top.api.domain.dto.PageDto;
import top.api.domain.pojo.Student;
import top.api.domain.pojo.User;
import top.api.domain.vo.PageVo;
import top.api.service.StudentService;
import top.api.mapper.StudentMapper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
* @author 86191
* @description 针对表【student】的数据库操作Service实现
* @createDate 2023-12-12 10:55:40
*/
@RequiredArgsConstructor
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
    implements StudentService{

    private  final  StudentMapper studentMapper;
    @Override
    public ResUlt<?> listPage(PageDto pageDto, Integer id, String name) {
        IPage<Student> page = new Page<>(pageDto.getPageNum(), pageDto.getPageSize());
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(id),Student::getId,id);
        lqw.like(ObjectUtils.isNotEmpty(name),Student::getName,name);
        studentMapper.selectPage(page,lqw);
        PageVo<Student> pageVo =new PageVo<>(page.getRecords(),page.getTotal());
        return ResUlt.success(pageVo);
    }

    @Override
    public ResUlt<?> add(Student student) {
        studentMapper.insert(student);
        return ResUlt.success();
    }

    @Override
    public ResUlt<?> delete(List id) {
        int ids = studentMapper.deleteBatchIds(id);
        return ResUlt.success(id);
    }

    @Override
    public ResUlt<?> changeStudent(Student student) {
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(student.getId()),Student::getId,student.getId());
        int update = studentMapper.update(student, lqw);
        if(update>0){
            return  ResUlt.success();
        }
          return  ResUlt.error();
    }

    @Override
    public ResUlt<?> changeStatus(Student student) {
        String loginId = StpUtil.getLoginId().toString();
        if("1".equals(loginId)){
            LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
            lqw.eq(ObjectUtils.isNotEmpty(student.getId()),Student::getId,student.getId());
            int update = studentMapper.update(student, lqw);
            if(update>0){
                return ResUlt.success("更改成功！");
            }
        }
        return ResUlt.error("该账户不是管理员，不可以解禁操作哦");
    }

    @Override
    public ResUlt<?> getOne(Integer id) {
        LambdaQueryWrapper<Student> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ObjectUtils.isNotEmpty(id),Student::getId,id);
        Student student = studentMapper.selectOne(lqw);
        return ResUlt.success(student);
    }
}




